package com.gzxxgl.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static Date getDate(String d, String fm){
		DateFormat format = new SimpleDateFormat(fm);         
		Date date = null;    
	         
		try {    
		           date = format.parse(d);   
		} catch (ParseException e) {    
		           e.printStackTrace();    
		}   
	
		return date;
	}
	
	public static String getString(Date d, String fm) {
		   SimpleDateFormat formatter = new SimpleDateFormat(fm);
		   String dateString = formatter.format(d);
		   return dateString;
		}
	/**
	 * string 日期格式转换
	 * @param d 
	 * @param ofm  老格式
 	 * @param nfm  新格式
	 * @return
	 */
	public static String getString(String d, String ofm, String nfm) {
		   return getString(getDate(d,ofm), nfm);
		}
	
	/**
	 * 返回  [年,月,日]
	 * @param d
	 * @return
	 */
	public static Integer[] getDate(Date d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		int year = cal.get(Calendar.YEAR);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		int month = cal.get(Calendar.MONTH) + 1;
		Integer[] s = new Integer[3];
		s[0] = year;s[1] = month;s[2] = day;
		return  s;
	}
	
	public static void main(String[] args) {
		 System.out.println(getString(getDate("20160623","yyyyMMdd"),"yyyy-MM-dd"));
	}
	
	/**
	 * 工龄
	 * @param rzrq
	 * @return
	 */
	public static String gl(Date rzrq){
		
		Calendar cal = Calendar.getInstance();

		int nyear = cal.get(Calendar.YEAR);

		cal.setTime(rzrq);

		int ryear = cal.get(Calendar.YEAR);
		int rmonth = cal.get(Calendar.MONTH) + 1;

		Integer year = null;

		year = nyear - ryear;
		if (rmonth >= 7) {
			if (year > 0) {
				year--;
			}
		}
		return year+"";
		
	}
}
